package com.wc.alorithm_luogu.P1088;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Arrays;

/**
 * @Author congege
 * @Description 火星人
 * https://www.luogu.com.cn/problem/P1088
 * @Date Created in 2023/9/18-14:46
 */
public class Main {
    public static void main(String[] args) {
        Input scan = new Input();
        int n = scan.nextInt();
        m = scan.nextInt();
        int[] fingersH = new int[n];

        for (int i = 0; i < n; i++) {
            fingersH[i] = scan.nextInt();
        }
        Main main = new Main();
        main.fullPermutation(new boolean[n + 1], new int[n], fingersH, 0);
        for (int i = 0; i < n; i++) {
            System.out.printf("%d%s", fingersH[i], i < n - 1 ? ' ' : "");
        }
    }

    static boolean flg = false;
    static int m;

    public void fullPermutation(boolean[] vis, int[] array, int[] compareArray, int floor) {
        if (floor == array.length) {
            if (flg) {
                m--;
            }
            if (Arrays.equals(array, compareArray)) {
                flg = true;
            }
            if (m == 0) {
                System.arraycopy(array, 0, compareArray, 0, array.length);
//                for (int i = 0; i < compareArray.length; i++) {
//                    System.out.printf("%d%s", compareArray[i], i < compareArray.length - 1 ? ' ' : "");
//                }
//                System.out.println("---------------------------");
            }
            return;
        }
        for (int i = 1; i <= array.length; i++) {
            if (m == 0) {
                break;
            }
            if (!vis[i]) {
                vis[i] = true;
                array[floor] = i;
                fullPermutation(vis, array, compareArray, floor + 1);
                vis[i] = false;
            }
        }
    }

    static class Input {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new BufferedReader(new InputStreamReader(System.in))));

        public int nextInt() {
            try {
                in.nextToken();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return (int) in.nval;
        }
    }
}
